package ljl.alg.wangzheng_camp.round1.tree;

import commons.TreeNode;

import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

/**
 * 目前收集了三个解法：
 * 1. 剑指 offer 的办法
 * 2. 迭代
 * 3. 记录索引
 */
public class _105_buildtree_preorder_inorder {
    
    class not_bad {
        public TreeNode buildTree(int[] preorder, int[] inorder) {
            return doBuild(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1);
        }
        TreeNode doBuild(int[] pre, int prel, int prer, int[] in, int inl, int inr) {
            if (prel > prer) return null;
            int rootVal = pre[prel];
            TreeNode root = new TreeNode(rootVal);
        
            // 这段也可以改成，用一个 hashmap 存中序遍历的 idx 直接拿到 p
            // 就不写了
            int p = inl;
            while (in[p] != rootVal) p++;
            int leftLen = p - inl;
        
            root.left = doBuild(pre, prel + 1, prel + leftLen, in, inl, inl + leftLen - 1);
            root.right = doBuild(pre, prel + leftLen + 1, prer, in, inl + leftLen + 1, inr);
            return root;
        }
    }
    
    /*
    * 确实快了，嘿！
    * */
    class little_improve {
        Map<Integer, Integer> inorderIndexMap = new HashMap<>();
        public TreeNode buildTree(int[] preorder, int[] inorder) {
            for (int i = 0; i < inorder.length; i++) {
                inorderIndexMap.put(inorder[i], i);
            }
            return doBuild(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1);
        }
        TreeNode doBuild(int[] pre, int prel, int prer, int[] in, int inl, int inr) {
            if (prel > prer) return null;
            int rootVal = pre[prel];
            TreeNode root = new TreeNode(rootVal);
            
            int leftLen = inorderIndexMap.get(rootVal) - inl;
            
            root.left = doBuild(pre, prel + 1, prel + leftLen, in, inl, inl + leftLen - 1);
            root.right = doBuild(pre, prel + leftLen + 1, prer, in, inl + leftLen + 1, inr);
            return root;
        }
    }
    
    /*
    * 笨逼的复刻巧妙法
    * 确实难，又玩了一波找不同
    * */
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        TreeNode root = new TreeNode(preorder[0]);
        LinkedList<TreeNode> stack = new LinkedList<>();
        stack.push(root);
        int inorderIndex = 0;
        for (int i = 1; i < preorder.length; i++) {
            TreeNode node = stack.peek();
            if (node.val != inorder[inorderIndex]) {
                node.left = new TreeNode(preorder[i]);
                stack.push(node.left);
            } else {
                while (!stack.isEmpty() && stack.peek().val == inorder[inorderIndex]) {
                    inorderIndex++;
                    node = stack.pop();
                }
                node.right = new TreeNode(preorder[i]);
                stack.push(node.right);
            }
        }
        return root;
    }
    /*
    * 巧妙法
    * */
    class qiao_miao_fa {
        public TreeNode buildTree(int[] preorder, int[] inorder) {
            TreeNode root = new TreeNode(preorder[0]);
            Deque<TreeNode> stack = new LinkedList<>();
            stack.push(root);
            int inorderIndex = 0;
            for (int i = 1; i < preorder.length; i++) {
                int preorderVal = preorder[i];
                TreeNode node = stack.peek();
                if (node.val != inorder[inorderIndex]) {
                    node.left = new TreeNode(preorderVal);
                    stack.push(node.left);
                }
                else {
                    while (!stack.isEmpty() && stack.peek().val == inorder[inorderIndex]) {
                        node = stack.pop();
                        inorderIndex++;
                    }
                    node.right = new TreeNode(preorderVal);
                    stack.push(node.right);
                }
            }
            return root;
        }
    }
    
}
